const path = require('path')
    // 导入HTML插件，得到一个构造函数
const HtmlPlugin = require('html-webpack-plugin')
    // 创建html插件的实例对象
const htmlPlugin = new HtmlPlugin({
        template: './src/index.html', //指定原文件存放的路径
        filename: './index.html', //指定生成文件路径
    })
    // 按需导入自动清理dist文件的构造函数插件(结构复制出)
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
    // 实例化对象
const cleanPlugin = new CleanWebpackPlugin()
module.exports = {
    mode: 'development', //用来指定构建的模式。可选值有production和development
    devtool: 'eval-source-map',
    plugins: [htmlPlugin, cleanPlugin], //通过plugin节点，使htmlPlugin插件生效

    // 指定打包路径入口函数
    entry: path.join(__dirname, './src/index.js'),
    // 指定打包的出口路径
    output: {
        path: path.join(__dirname, 'dist'),
        filename: 'js/main.js' //自动将打包好的js文件放到js文件包中
    },
    devServer: {
        open: true,
        host: '127.0.0.1',
        port: 80
    },
    module: {
        rules: [{
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            },
            {
                test: /\.less$/,
                use: ['style-loader', 'css-loader', 'less-loader']
            },
            // {
            //     test: /\.jpg|png|jif$/,
            //     use: ['url-loader?limit=22229']
            // }
            {
                test: /\.jpg|png|jif$/,
                use: {
                    loader: 'url-loader',
                    options: {
                        limit: 30000,
                        outputPath: 'image'
                    }
                }
            },

            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        plugins: ['@babel/plugin-proposal-class-properties']
                    }
                }
            }
        ]
    }
}